package com.crane.oss.web.controller.sports;

import com.crane.common.core.dto.SingleResponse;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.sports.XunFeiPrescriptionApiService;
import com.crane.oss.xunfei.req.QueryPrescriptionClassInfoReq;
import com.crane.oss.xunfei.req.QueryPrescriptionClassStudentInfoReq;
import com.crane.oss.xunfei.req.QueryPrescriptionStudentInfoReq;
import com.crane.oss.sports.bean.response.exerciseprescription.*;
import com.crane.oss.sports.bean.resquest.*;
import com.crane.oss.web.BaseBusinessController;
import com.crane.oss.xunfei.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(value = "ExercisePrescriptionController", tags = {"智慧体育-运动处方"})
@RestController
@RequestMapping("/sports/exercise/prescription")
public class ExercisePrescriptionController extends BaseBusinessController {

    @Autowired
    XunFeiPrescriptionApiService xunFeiPrescriptionApiService;

    @ApiOperation(value = "班级运动处方", notes = "班级运动处方")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryClassExercisePrescriptionReq")
    @PostMapping("/class")
    public SingleResponse<ClassPrescriptionResp> classList(@RequestBody QueryClassExercisePrescriptionReq request) {
        ClassPrescriptionResp classPrescriptionResp=new ClassPrescriptionResp();
        QueryPrescriptionClassInfoReq queryTestCycleReq =new QueryPrescriptionClassInfoReq();
        queryTestCycleReq.setClassId(request.getClassId());
        queryTestCycleReq.setSportCycleId(request.getCycleId());
        ApiSingleResponse<PrescriptionClassInfoResp> prescriptionClassInfo= xunFeiPrescriptionApiService.getPrescriptionClassInfo(queryTestCycleReq,getPartyId().toString(), null,getUserId().toString());
        if(prescriptionClassInfo.getMsg().equals("success")){
            classPrescriptionResp.setAvgScore(prescriptionClassInfo.getData().getAvgScore());
            classPrescriptionResp.setAvgAddScore(prescriptionClassInfo.getData().getAvgAddScore());
            classPrescriptionResp.setSchoolExcellentScore(prescriptionClassInfo.getData().getSchoolExcellentScore());
            classPrescriptionResp.setSchoolPassScore(prescriptionClassInfo.getData().getSchoolPassScore());
        }

        ApiListResponse<PrescriptionBmiResp> prescriptionBmiRespApiListResp= xunFeiPrescriptionApiService.getPrescriptionBmi(queryTestCycleReq,getPartyId().toString(), null,getUserId().toString());
        if(prescriptionBmiRespApiListResp.getMsg().equals("success")){
            Map<String,LevelBmiBean> bmi=new HashMap<>();
            List<LevelBmiBean> bmiList= BeanUtil.copyList(prescriptionBmiRespApiListResp.getData(),LevelBmiBean.class);
            for(LevelBmiBean levelBmiBean : bmiList){
                if(levelBmiBean.getLevelKey().equals("肥胖")){
                    bmi.put("fat",levelBmiBean);
                }else if(levelBmiBean.getLevelKey().equals("超重")){
                    bmi.put("more",levelBmiBean);
                }else if(levelBmiBean.getLevelKey().equals("低体重")){
                    bmi.put("low",levelBmiBean);
                }else if(levelBmiBean.getLevelKey().equals("正常")){
                    bmi.put("normal",levelBmiBean);
                }
            }
            classPrescriptionResp.setBmi(bmi);
        }
        ApiListResponse<PrescriptionItemResp> itemResp= xunFeiPrescriptionApiService.getPrescriptionItem(queryTestCycleReq,getPartyId().toString(), null,getUserId().toString());
        if(itemResp.getMsg().equals("success")){
            List<ItemScoreBean> itemScore=itemResp.getData().stream().map(s ->{
                ItemScoreBean a=new ItemScoreBean();
                a.setItemName(s.getItemName());
                a.setFail(s.getFail());
                a.setAvgScore(s.getAvgScore());
                a.setPass(s.getPass());
                a.setExcellent(s.getExcellent());
                a.setGood(s.getGood());
                return  a;
            }).collect(Collectors.toList());
            classPrescriptionResp.setItemScore(itemScore);
        }
        return SingleResponse.of(classPrescriptionResp);
    }



    @ApiOperation(value = "班级运动处方-学生列表", notes = "班级运动处方-学生列表")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryClassPrescriptionStudentReq")
    @PostMapping("/open/teacher/courseDetail")
    public SingleResponse openTeacherCourseDetail(@RequestBody QueryClassPrescriptionStudentReq request) {
        Map<String,List<StudentPrescriptionBean>> result=new HashMap<>();
        QueryPrescriptionClassStudentInfoReq queryOpenCourseDetailTeacherReq=new QueryPrescriptionClassStudentInfoReq();
        queryOpenCourseDetailTeacherReq.setType(request.getType());
        queryOpenCourseDetailTeacherReq.setClassId(request.getClassId());
        queryOpenCourseDetailTeacherReq.setSportCycleId(request.getCycleId());
        ApiListResponse<PrescriptionClassStudentInfoResp> response=xunFeiPrescriptionApiService.getPrescriptionStudent(queryOpenCourseDetailTeacherReq,getPartyId().toString(),null,getUserId().toString());
        if(response.getMsg().equals("success")){
            List<StudentPrescriptionBean> respList=response.getData().stream().map(s ->{
                StudentPrescriptionBean a=new StudentPrescriptionBean();
                BeanUtil.copyProperties(s,a);
                return  a;
            }).collect(Collectors.toList());
            if(request.getType() == 1){
                for(StudentPrescriptionBean studentPrescriptionBean : respList){
                    if(studentPrescriptionBean.getLevel().equals("不及格")){
                        if(null ==result.get("fail")){
                            List<StudentPrescriptionBean> list=new ArrayList<>();
                            result.put("fail",list);
                        }
                        result.get("fail").add(studentPrescriptionBean);
                    }else if(studentPrescriptionBean.getLevel().equals("及格")){
                        if(null ==result.get("pass")){
                            List<StudentPrescriptionBean> list=new ArrayList<>();
                            result.put("pass",list);
                        }
                        result.get("pass").add(studentPrescriptionBean);
                    }else if(studentPrescriptionBean.getLevel().equals("良好")){
                        if(null ==result.get("good")){
                            List<StudentPrescriptionBean> list=new ArrayList<>();
                            result.put("good",list);
                        }
                        result.get("good").add(studentPrescriptionBean);
                    }else if(studentPrescriptionBean.getLevel().equals("优秀")){
                        if(null ==result.get("excellent")){
                            List<StudentPrescriptionBean> list=new ArrayList<>();
                            result.put("excellent",list);
                        }
                        result.get("excellent").add(studentPrescriptionBean);
                    }
                }
            }else if(request.getType() == 2){
                result.put("data",respList);
            }else{
                result=respList.stream().collect(Collectors.groupingBy(StudentPrescriptionBean ::getItemName));
            }
        }
        return SingleResponse.of(result);
    }


    @ApiOperation(value = "个人运动处方", notes = "个人运动处方")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryStudentExercisePrescriptionReq")
    @PostMapping("/student")
    public SingleResponse<String> studentInfo(@RequestBody QueryStudentExercisePrescriptionReq request) {
        QueryPrescriptionStudentInfoReq queryPrescriptionStudentInfoReq=new QueryPrescriptionStudentInfoReq();
        queryPrescriptionStudentInfoReq.setStudentId(request.getStudentId());
        queryPrescriptionStudentInfoReq.setSportCycleId(request.getCycleId());
        ApiSingleResponse<String> response=xunFeiPrescriptionApiService.getPrescriptionClassInfo(queryPrescriptionStudentInfoReq,getPartyId().toString(), null,getUserId().toString());
        if(null != response && response.getMsg().equals("success")){
            return SingleResponse.of(response.getData());
        }
        return SingleResponse.buildFailure("请求失败");
    }




}
